class Solution {
public:
    string convert(string s, int numRows)
    {
        if (numRows == 1) return s;

        string ret;
        int d = 2 * numRows - 2;
        int len = s.size();

        for (int i = 0; i < len; i += d) ret += s[i];
        for (int i = 1; i < numRows - 1; i++)
        {
            for (int j = i, k = d - i; j < len || k < len; j += d, k += d)
            {
                if (j < len) ret += s[j];
                if (k < len) ret += s[k];
            }
        }
        for (int i = numRows - 1; i < len; i += d)  ret += s[i];

        return ret;
    }
};